package com.ethan.rabbittopicproducer;

import lombok.Data;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
@Data
public class MyBean {
    private final AmqpAdmin amqpAdmin;
    private final AmqpTemplate amqpTemplate;

    @Autowired
    public MyBean(AmqpAdmin amqpAdmin, AmqpTemplate amqpTemplate) {
        this.amqpAdmin = amqpAdmin;
        this.amqpTemplate = amqpTemplate;
    }
    //声明队列
    @Bean("queue")
    Queue queue(){
        return QueueBuilder.durable("queue_topics").build();
    }
    //声明队列1
    @Bean("queue01")
    Queue queue01(){
        return QueueBuilder.durable("queue_topics_01").build();
    }
    //声明路由注意路由的类型为direct
    @Bean
    Exchange exchange(){
        return ExchangeBuilder.topicExchange("ethan.exchange_topics").build();
    }
    //绑定队列到路由并设置routingkey为routing_inforouting_error
    @Bean
    Binding binding(@Qualifier("queue") Queue queue,
                    Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("*.bule").noargs();
    }
    //绑定队列1到路由并设置routingkey为routing_inforouting_error
    @Bean
    Binding binding01(@Qualifier("queue01") Queue queue,
                    Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("*.bule.*").noargs();
    }
    //绑定队列1到路由并设置routingkey为routing_info
    @Bean
    Binding binding02(@Qualifier("queue01") Queue queue,
                      Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("red.#").noargs();
    }
}
